package com.spring.gop.proj.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.gop.proj.interfaces.QnaDao;
import com.spring.gop.proj.interfaces.QnaService;
import com.spring.gop.proj.model.Cdt;
import com.spring.gop.proj.model.Qna;

@Service
public class QnaServiceImpl implements QnaService {

	@Autowired QnaDao qnaDao;
	
	public List<Qna> getArticles() {
		return qnaDao.getArticles();
	}
	
	public Qna getArticle(int q_num) {
		qnaDao.updateHit(q_num);
		Qna qna = qnaDao.getArticle(q_num);
		String contentTxt = qna.getContent().replaceAll("\r\n", "<br/>");
		qna.setContent(contentTxt);
		return qna;
	}
	
	@Transactional
	public void insertArticle(Qna qna){
		int q_num=qna.getQ_num();
		int ref=qna.getRef();
		int step=qna.getStep();
		int depth=qna.getDepth();
		int number=0;
		try{
			number=qnaDao.getMaxNum() +1;
			if(qnaDao.getMaxNum()!=0) number=qnaDao.getMaxNum()+1;
			else number=1;
			if(q_num!=0){
				qnaDao.updateNum(qna);
				step=step+1;
				depth=depth+1;
				qna.setStep(step);
				qna.setDepth(depth);
			}else{
				ref=number;
				qna.setRef(ref);
				step=0;
				depth=0;
				qna.setStep(step);
				qna.setDepth(depth);
			}
				qnaDao.insertArticle(qna);
			}catch(Exception e){
				e.printStackTrace();
		}
	}
	
	@Transactional
	public int updateArticle(Qna qna){
		String dbpasswd="";
		int result=-1;
		try{
			dbpasswd=qnaDao.getPasswd(qna.getQ_num());
			if(dbpasswd.equals(qna.getPass())){
				qnaDao.updateArticle(qna);
				result=1;
			}else result=0;
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	@Transactional
	public int delArticle(int q_num, String pass){
		String dbpasswd="";
		int result=-1;
		System.out.println(pass);
		try{
			dbpasswd=qnaDao.getPasswd(q_num);
			if(dbpasswd.equals(pass)){
				qnaDao.delArticle(q_num);
				result=1;
			}else result=0;
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}
	
	/*@Transactional
	public Qna getPasswd(int num){
		return qnaDao.getPasswd(num);
	}
	*/
	@Transactional
	public void updateNum(Qna qna){
		qnaDao.updateNum(qna);
	}
	
	@Transactional
	public int getMaxNum(){
		return qnaDao.getMaxNum();
	}
	
	@Transactional
	public int getArticleCount(){
		int x=0;
		try{
			qnaDao.getArticleCount();
			if(qnaDao.getArticleCount()!=0){
				x=qnaDao.getArticleCount();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return x;
	}
	
	@Transactional
	public void updateHit(int q_num){
		qnaDao.updateHit(q_num);
	}
	
	@Transactional
	public List<Qna> getArticles1(Cdt cdt){
		List<Qna> articleList=null;
		try{
			articleList=qnaDao.getArticles1(cdt);
		}catch(Exception e){
			e.printStackTrace();
		}
		return articleList;
	}
	
	@Transactional
	public int getArticleCount1(Cdt cdt){
		int x=0;
		x=qnaDao.getArticleCount1(cdt);
		return x;
	}
	
	@Transactional
	public List<Qna> getArticles2(Cdt cdt){
		List<Qna> articleList=null;
		articleList=qnaDao.getArticles2(cdt);
		return articleList;
	}
	
	public List<Qna> selectListForId(String member_id) {
		return qnaDao.selectListForId(member_id);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	public List<Qna> getArticles3() {
		return qnaDao.getArticles3();
	}
	
	public Qna getArticle1(int q_num) {
		qnaDao.updateHit1(q_num);
		Qna qna = qnaDao.getArticle1(q_num);
		String contentTxt = qna.getContent().replaceAll("\r\n", "<br/>");
		qna.setContent(contentTxt);
		return qna;
	}
	
	@Transactional
	public void insertArticle1(Qna qna){
		qnaDao.insertArticle1(qna);
		/*int q_num=qna.getQ_num();
		int ref=qna.getRef();
		int step=qna.getStep();
		int depth=qna.getDepth();
		int number=0;
		try{
			number=qnaDao.getMaxNum1() +1;
			if(qnaDao.getMaxNum1()!=0) number=qnaDao.getMaxNum1()+1;
			else number=1;
			if(q_num!=0){
				qnaDao.updateNum1(qna);
				step=step+1;
				depth=depth+1;
				qna.setStep(step);
				qna.setDepth(depth);
			}else{
				ref=number;
				qna.setRef(ref);
				step=0;
				depth=0;
				qna.setStep(step);
				qna.setDepth(depth);
			}
				qnaDao.insertArticle1(qna);
			}catch(Exception e){
				e.printStackTrace();
		}*/
	}
	
	@Transactional
	public int updateArticle1(Qna qna){
		String dbpasswd="";
		int result=-1;
		try{
			dbpasswd=qnaDao.getPasswd1(qna.getQ_num());
			if(dbpasswd.equals(qna.getPass())){
				qnaDao.updateArticle1(qna);
				result=1;
			}else result=0;
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	@Transactional
	public int delArticle1(int q_num, String pass){
		String dbpasswd="";
		int result=-1;
		System.out.println(pass);
		try{
			dbpasswd=qnaDao.getPasswd1(q_num);
			if(dbpasswd.equals(pass)){
				qnaDao.delArticle1(q_num);
				result=1;
			}else result=0;
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(result);
		return result;
	}
	
	/*@Transactional
	public Qna getPasswd(int num){
		return qnaDao.getPasswd(num);
	}
	*/
	@Transactional
	public void updateNum1(Qna qna){
		qnaDao.updateNum1(qna);
	}
	
	@Transactional
	public int getMaxNum1(){
		return qnaDao.getMaxNum1();
	}
	
	@Transactional
	public int getArticleCount2(){
		int x=0;
		try{
			qnaDao.getArticleCount2();
			if(qnaDao.getArticleCount2()!=0){
				x=qnaDao.getArticleCount2();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return x;
	}
	
	@Transactional
	public void updateHit1(int q_num){
		qnaDao.updateHit1(q_num);
	}
	
	@Transactional
	public int getArticleCount3(Cdt cdt){
		int x=0;
		x=qnaDao.getArticleCount3(cdt);
		return x;
	}
	
	@Transactional
	public List<Qna> getArticles4(Cdt cdt){
		List<Qna> articleList=null;
		articleList=qnaDao.getArticles4(cdt);
		return articleList;
	}
	
	
	
	
	
	@Transactional
	public List<Qna> getArticles5(Cdt cdt){
		List<Qna> articleList=null;
		articleList=qnaDao.getArticles5(cdt);
		return articleList;
	}
	
}

